/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import tpa.TablaPeriodica.Elemento;

/**
 *
 * @author leandro
 */
public class TablaPeriodicaActionForm extends org.apache.struts.action.ActionForm
{

    private String _status;
    private List _elementos;
    private Long _numeroAtomico = 0L;
    private String[] grupos = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII"};
    private String[] periodos = {"1", "2", "3", "4", "5", "6", "7"};

    /**
     * @return the grupos
     */
    public String[] getGrupos()
    {
        return grupos;
    }

    /**
     * @return the periodos
     */
    public String[] getPeriodos()
    {
        return periodos;
    }

    /**
     * @return
     */
    public String getStatus()
    {
        return _status;
    }

    /**
     * @param string
     */
    public void setStatus(String string)
    {
        _status = string;
    }

    /**
     * @return the _elementos
     */
    public List getElementos()
    {
        if (_elementos == null)
        {
            _elementos = new ArrayList();
        }

        return _elementos;
    }

    /**
     * @param elementos the _elementos to set
     */
    public void setElementos(List elementos)
    {
        this._elementos = elementos;
    }

    /**
     * @return the _numeroAtomico
     */
    public Long getNumeroAtomico()
    {
        return _numeroAtomico;
    }

    /**
     * @param numeroAtomico the _numeroAtomico to set
     */
    public void setNumeroAtomico(Long numeroAtomico)
    {
        this._numeroAtomico = numeroAtomico;
    }


    public List getFamilias() throws  NamingException{
        return TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerClasificacion("Familia").getSubClases();
    }

    /**
     *
     */
    public TablaPeriodicaActionForm()
    {
        super();
    // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();
        return errors;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        try
        {
            this._elementos = TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerElementos();
        }
        catch (NamingException ex)
        {
            this._status = ex.getExplanation();
        }
    }

    public Elemento getElemento(String grupo, String periodo)
    {
        for(Object currentObject : this.getElementos())
        {
            Elemento currentElemento = (Elemento)currentObject;
            
            if(currentElemento.getProperty("Grupo") != null && currentElemento.getProperty("Grupo").getValor().equals(grupo) &&
                    currentElemento.getProperty("Periodo") != null && currentElemento.getProperty("Periodo").getValor().equals(periodo))
                return currentElemento;
        }
        
        return null;
    }
}
